package com.econ.powercloud.ems.timing.compute.strategy;

import com.econ.powercloud.ems.common.enums.DitTypeEnum;
import com.econ.powercloud.ems.constant.SysConstant;
import com.econ.powercloud.ems.dal.entity.CalCode;
import com.econ.powercloud.ems.dal.entity.EmsComputeConfig;
import com.econ.powercloud.ems.pojo.FlashDataVO;
import com.econ.powercloud.ems.pojo.QueryDataVO;
import org.apache.commons.collections.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author : zjf
 * @Description :
 * @Date : 2022/11/25 11:31
 * @Modified by : zjf
 */
public abstract class AbstractComputeCodeProcess {

    /**
     * 计算单元id
     */
    private Integer computeUnitId;

    /**
     * 处理数据,最终返回计算数值
     * @param emsComputeConfig : 计算点号配置
     * @param hasCalculateVal : 已计算的点号
     * @return
     */
    public abstract Double compute(EmsComputeConfig emsComputeConfig, Map<String,Double> hasCalculateVal,Long startTime);

    public Integer getComputeUnitId() {
        return computeUnitId;
    }

    public void setComputeUnitId(Integer computeUnitId) {
        this.computeUnitId = computeUnitId;
    }

    /**
     * 组装需要查询的实时数据
     */
    public List<QueryDataVO> getQueryDataListByCalCodeList(List<CalCode> calCodeList){
        if(CollectionUtils.isEmpty(calCodeList)){
            return Collections.emptyList();
        }
        return calCodeList.stream().map(d-> QueryDataVO.builder().pid(d.getPid()).deviceCode(d.getDeviceCode()).initDeviceCode(d.getDeviceCode())
                        .dit(d.getPid().replace(d.getDeviceCode() + SysConstant.SPOT,"")).build())
                .collect(Collectors.toList());
    }

    /**
     * 通过条件获取参与计算点号的值
     * @param calCode : 参与计算点号
     * @param hasCalculateVal : 已计算的点号
     * @param flashDataMap : 实时数据map
     * @param calDataMap : 计算数据map
     * @return
     */
    public Double getCalCodeVal(CalCode calCode,Map<String,Double> hasCalculateVal,Map<String, FlashDataVO> flashDataMap,
                                Map<String,FlashDataVO> calDataMap){
        if(hasCalculateVal.containsKey(calCode.getPid())){
            return hasCalculateVal.get(calCode.getPid());
        }else if(DitTypeEnum.isFlashData(calCode.getDitType())){
            return flashDataMap.get(calCode.getPid()) == null? null : Double.parseDouble(flashDataMap.get(calCode.getPid()).getVal());
        }else if(DitTypeEnum.isCalData(calCode.getDitType())) {
            return calDataMap.get(calCode.getPid()) == null? null : Double.parseDouble(calDataMap.get(calCode.getPid()).getVal());
        }
        return null;
    }

    /**
     * 计算值保留小数
     * @param decimalNumber
     * @return
     */
    public Double keepDecimalPoint(Integer decimalNumber,Double val){
        if(decimalNumber == null){
            decimalNumber = 0;
        }
        return BigDecimal.valueOf(val).setScale(decimalNumber, RoundingMode.HALF_UP).doubleValue();
    }
}
